package com.zsyh.jiujiu.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageOrderBean;
import com.zsyh.jiujiu.bean.MyGoodBean;
import com.zsyh.jiujiu.bean.MyInfoBeeean;
import com.zsyh.jiujiu.home.ClassFicationActivity;
import com.zsyh.jiujiu.home.CxRenzhengActivity;
import com.zsyh.jiujiu.home.GoodManagerActivity;
import com.zsyh.jiujiu.home.LoginActivity;
import com.zsyh.jiujiu.home.MyInfoActivity;
import com.zsyh.jiujiu.home.MyOrderActivity;
import com.zsyh.jiujiu.home.MySignActivity;
import com.zsyh.jiujiu.home.MyWuliuactivity;
import com.zsyh.jiujiu.home.MyvipActtivitty;
import com.zsyh.jiujiu.home.SettingActivity;
import com.zsyh.jiujiu.home.ZhiNanActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.heard)
    ImageView heard;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.revip)
    RelativeLayout revip;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tvall)
    TextView tvall;
    @BindView(R.id.reallorder)
    RelativeLayout reallorder;
    @BindView(R.id.xian)
    View xian;
    @BindView(R.id.redhf)
    RelativeLayout redhf;
    @BindView(R.id.redqr)
    RelativeLayout redqr;
    @BindView(R.id.redfh)
    RelativeLayout redfh;
    @BindView(R.id.redsh)
    RelativeLayout redsh;
    @BindView(R.id.resh)
    RelativeLayout resh;
    @BindView(R.id.refuwu)
    RelativeLayout refuwu;
    @BindView(R.id.xianfuwu)
    View xianfuwu;
    @BindView(R.id.resign)
    RelativeLayout resign;
    @BindView(R.id.remanager)
    RelativeLayout remanager;
    @BindView(R.id.rereenzheng)
    RelativeLayout rereenzheng;
    @BindView(R.id.rezhinan)
    RelativeLayout rezhinan;
    @BindView(R.id.rewuliu)
    RelativeLayout rewuliu;
    @BindView(R.id.rekf)
    RelativeLayout rekf;
    @BindView(R.id.resetting)
    RelativeLayout resetting;
    @BindView(R.id.reloginout)
    RelativeLayout reloginout;
    private String qqnumnber;
    private String shangnum,xianum,shenghemun,quxiaonum;

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.heard, R.id.revip, R.id.reallorder, R.id.redhf, R.id.redqr, R.id.redfh, R.id.redsh, R.id.resh, R.id.resign, R.id.remanager, R.id.rereenzheng, R.id.rezhinan, R.id.rewuliu, R.id.rekf, R.id.resetting, R.id.reloginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.heard:
                ActivityUtils.startActivity(MyInfoActivity.class);
                break;
            case R.id.revip:
                ActivityUtils.startActivity(MyvipActtivitty.class);
                break;
            case R.id.reallorder:
                Bundle bundleall = new Bundle();
                bundleall.putString("name", "0");
                ActivityUtils.startActivity(bundleall, MyOrderActivity.class);
                break;
            case R.id.redhf:
                Bundle bundlefahuo = new Bundle();
                bundlefahuo.putString("name", "1");
                ActivityUtils.startActivity(bundlefahuo, MyOrderActivity.class);

                break;
            case R.id.redqr:
                Bundle bundleque = new Bundle();
                bundleque.putString("name", "2");
                ActivityUtils.startActivity(bundleque, MyOrderActivity.class);
                break;
            case R.id.redfh:
                Bundle bundlefa = new Bundle();
                bundlefa.putString("name", "3");
                ActivityUtils.startActivity(bundlefa, MyOrderActivity.class);
                break;
            case R.id.redsh:
                Bundle bundleshouhuo = new Bundle();
                bundleshouhuo.putString("name", "4");
                ActivityUtils.startActivity(bundleshouhuo, MyOrderActivity.class);
                break;
            case R.id.resh:
                //ActivityUtils.startActivity(MyOrderActivity.class);
                break;
            case R.id.resign:
                ActivityUtils.startActivity(MySignActivity.class);
                break;
            case R.id.remanager:
                Bundle guanli = new Bundle();
                guanli.putString("shangnum", shangnum);
                guanli.putString("xianum", xianum);
                guanli.putString("shenghemun", shenghemun);
                guanli.putString("quxiaonum", quxiaonum);
                ActivityUtils.startActivity(guanli,GoodManagerActivity.class);
                break;
            case R.id.rereenzheng:
                ActivityUtils.startActivity(CxRenzhengActivity.class);
                break;
            case R.id.rezhinan:
                ActivityUtils.startActivity(ZhiNanActivity.class);
                break;
            case R.id.rewuliu:
                ActivityUtils.startActivity(MyWuliuactivity.class);
                break;
            case R.id.rekf:
                if(isQQClientAvailable(getActivity())){
                    final String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=1149545660&version=1";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
                }else{
                    Toast.makeText(getActivity(),"请安装QQ客户端", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.resetting:
                ActivityUtils.startActivity(SettingActivity.class);

                break;
            case R.id.reloginout:
                SPUtils.put(getActivity(),"userid","");
                EMClient.getInstance().logout(true, new EMCallBack() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError(int i, String s) {

                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
                ActivityUtils.startActivity(LoginActivity.class);
                break;
        }
    }

    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        gettUser((String) SPUtils.get(getActivity(), "userid", ""));
        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");
        MyGoodxia((String) SPUtils.get(getActivity(), "userid", ""),"2");
        shenghe((String) SPUtils.get(getActivity(), "userid", ""),"3");
        quxiaole((String) SPUtils.get(getActivity(), "userid", ""),"4");
    }

    /**
     * 我的商品
     */
    public void MyGood(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        shangnum=list.size()+"";
                    }
                });

    }


    /**
     * 我的商品
     */
    public void MyGoodxia(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        xianum=list.size()+"";
                    }
                });

    }

    public void shenghe(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        shenghemun=list.size()+"";
                    }
                });

    }

    public void quxiaole(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        List<MyGoodBean.DataBean> list=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                        }
                        quxiaonum=list.size()+"";
                    }
                });

    }

    /**
     * getuser
     */
    public void gettUser(String userid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getUserById(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("Myinfo", JsonStr + "");
                        Gson gson = new Gson();
                        MyInfoBeeean myInfoBeeean = gson.fromJson(JsonStr, MyInfoBeeean.class);
                        name.setText(myInfoBeeean.getData().getUserName());
                        phone.setText(myInfoBeeean.getData().getPhone());
                        Glide.with(getActivity())
                                .load(myInfoBeeean.getData()
                                        .getHeadImg())
                                .transform(new GlideCircleTransform(getActivity()))
                                .into(heard);


                    }
                });
    }

}
